package com.system.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.system.entity.User;
import com.system.service.IUserService;
import com.system.service.UserServiceImpl;
import com.system.util.LoginData;


public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public LoginServlet() {
    	
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		User user = new User(username, password);
		IUserService userService = new UserServiceImpl();
		String name = userService.checkUser(user);
		
		PrintWriter out = response.getWriter();
		if (name != null && name.equals(username)) {
			LoginData datas = new LoginData("1", name);
			String data = JSON.toJSONString(datas);
			out.print(data);
		}else {
			LoginData datas = new LoginData("0");
			datas.setName(null);
			String data = JSON.toJSONString(datas);
			out.print(data);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
